package com.lbb.springboot.auto.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;

public class DoHtmlForm {
	String encoding = "UTF-8";
	List<HashMap<String, String>> cfg = new ArrayList<HashMap<String, String>>();
	static String datafile = "";
	static String actionpath = "";
	static String htmlformpath = "";
	List<String> list = new ArrayList<String>();

	public static final void main(String args[]) throws IOException {
		int type = 1;
		if (type == 1) { // app模板
			datafile = "data2.txt";
			System.out.println(datafile);
//			actionpath = "app";
			htmlformpath = "test";
		} else if (type == 1) {
			datafile = "data2.txt";
//			actionpath = "web";
			htmlformpath = "testformweb";
		} else {
			datafile = "";
			actionpath = "";
			htmlformpath = "";
			return;
		}

		DoHtmlForm test = new DoHtmlForm();
		test.test();
	}

	public void test() throws IOException {
		StringBuffer sb = new StringBuffer();
		String filepath = Thread.currentThread().getContextClassLoader()
				.getResource(".").getPath();
		
		System.out.println("filepath:"+filepath);
		
		filepath=filepath.substring(0, filepath.indexOf("/target"));
		System.out.println("filepath2:"+filepath);
		
		File file = new File(filepath + "\\src\\main\\webapp\\00\\" + datafile);
		System.out.println(filepath + "--" + file.exists());

		InputStreamReader read = new InputStreamReader(
				new FileInputStream(file), encoding);
		
		
		BufferedReader bufferedReader = new BufferedReader(read);
		String lineTxt = null;
		while ((lineTxt = bufferedReader.readLine()) != null) {
			System.out.println(lineTxt);
			String line = lineTxt.trim();
			if (!line.startsWith("//")) {
				if (line.endsWith("}")) {
					sb.append(line);
					list.add(sb.toString());
					sb.delete(0, sb.length());
				} else {
					sb.append(line);
				}
			}
		}
		read.close();

		System.out.println(new Gson().toJson(list));
		for (int i = 0; i < list.size(); i++) {
			String one = list.get(i);
			FormData fd = new FormData(one);
			System.out.println(new Gson().toJson(fd));
			// makejavaclass(fd);
			makehtmlform(fd);
		}

		makeindex();
	}

	public void makejavaclass(FormData data) {
		System.out.println("创建java类：" + data.classname);
		String filepath = Thread.currentThread().getContextClassLoader()
				.getResource(".").getPath();
		// LOG.out(filepath);
		File file = new File(filepath + "../../src/dto/" + data.getModelname()
				+ "/" + FormData.firstUpper(data.classname) + ".java");

		if (file.exists()) {
			file.delete();
		}

		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdir();
		}

		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}

		BufferedWriter writer;
		try {
			writer = new BufferedWriter(
					new FileWriter(new File(file.getPath())));
			writer.write(data.tojavaString("dto." + data.getModelname()));
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void makehtmlform(FormData data) {
		System.out.println("创建html测试表单：" + data.classname);
		String filepath = Thread.currentThread().getContextClassLoader()
				.getResource(".").getPath();
		// LOG.out(filepath);
		

		filepath=filepath.substring(0, filepath.indexOf("/target"));
		
		File file = new File(filepath + "\\src\\main\\webapp\\" + htmlformpath + "/"
				+ data.classname + ".jsp");
		
		
		if (file.exists()) {
			file.delete();
		}
		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}

		BufferedWriter writer;
		try {
			writer = new BufferedWriter(
					new FileWriter(new File(file.getPath())));
			writer.write(data.tohtmlform("../" + actionpath));
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void makeindex() {
		String pre = "\\src\\main\\webapp\\" + htmlformpath + "/";
		System.out.println("创建index.html测试：");
		String filepath = Thread.currentThread().getContextClassLoader()
				.getResource(".").getPath();
		// LOG.out(filepath);
		

		filepath=filepath.substring(0, filepath.indexOf("/target"));
		
		System.out.println("======================================"+filepath + pre + "index.jsp");
		File file = new File(filepath + pre + "index.jsp");
		if (file.exists()) {
			file.delete();
		}
		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}

		File parent = new File(filepath + pre);
		File[] lf = parent.listFiles(new FilenameFilter() {

			public boolean accept(File dir, String name) {
				if (name.endsWith(".jsp")
						&& !name.equalsIgnoreCase("index.jsp")) {
					return true;
				}
				return false;
			}
		

		});

		StringBuffer sb = new StringBuffer();

		sb.append("<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>");
		sb.append("<%@ page pageEncoding='UTF-8' contentType='text/html; charset=UTF-8'%>");
		sb.append("<%@ page session='true'%>");
		sb.append("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>");
		sb.append("<html>").append("\r");
		sb.append("<head>\r");
		sb.append("<meta http-equiv='pragma' content='no-cache'> \r");
		sb.append("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
		sb.append("</head>\r");
		sb.append("<body>").append("\r");
		sb.append("\t<a href='http://note.youdao.com/yws/public/redirect/share?id=39ece88f92578c1ee648bb946128ae57&type=false'>接口使用说明</a><p>\r");
		sb.append("\t<a href='../00/" + datafile + "'>接口声明文件</a><p>\r");
		for (int i = 0; i < lf.length; i++) {
			sb.append("\t<a href='" + lf[i].getName() + "'>" + lf[i].getName()
					+ "</a><p>\r");
		}
		sb.append("</body>").append("\r");
		sb.append("</html>");
		BufferedWriter writer;
		try {
			writer = new BufferedWriter(
					new FileWriter(new File(file.getPath())));
			writer.write(sb.toString());
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
